/* -+- file  ./kf2/Roster/Group.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections;
using System.Collections.Generic;

namespace Habazie.kf2.Roster
{
	///<summary>Roster group</summary>
	public class Group : IEnumerable, GUI.IRosterItem
	{
		///<summary>Group name</summary>
		private string m_Name;
		
		///<summary>List of items in this group</summary>
		private LinkedList<Item> m_Items;
		
		private int m_Count = 0;
		private int m_Online = 0;
		
		///<summary></summary>
		public Group(string name)
		{
			m_Name = name;
			m_Items = new LinkedList<Item> ();
		}
		
		public string Name {
			get { return m_Name; }
		}
		
		public IEnumerator GetEnumerator ()
		{
			return m_Items.GetEnumerator ();
		}
		
		///<summary>Update group counters</summary>
		public void Update ()
		{
			m_Count = 0;
			m_Online = 0;
			
			foreach (Item item in m_Items) {
				m_Count++;
				if (item.Resource.Presence == jabber.protocol.client.PresenceType.available)
					m_Online++;
			}
		}
		
		
		///<summary>Add item to this group</summary>
		public void Add (Item item)
		{
			m_Items.AddLast (item);
			Update ();
		}
		
		
		///<summary>Remove item form this group</summary>
		public void Remove (Item item)
		{
			m_Items.Remove (item);
			Update ();
		}
		
		public void DisplayAtIter (Gtk.TreeModel model, Gtk.TreeIter iter)
		{
			model.SetValue (iter, 0, this);
		//	model.SetValue (iter, 1, null);
			model.SetValue (iter, 2, String.Format ("{0} ({1}/{2})",
										m_Name, m_Online, m_Count));
		//	model.SetValue (iter, 3, null);
		}

	}
}
